package com.kalix.training.hotlists.biz;

import com.kalix.framework.core.api.persistence.JsonData;
import com.kalix.framework.core.impl.biz.ShiroGenericBizServiceImpl;
import com.kalix.training.hotlists.api.biz.IHotlistsCourseBeanService;
import com.kalix.training.hotlists.api.dao.IHotlistsCourseBeanDao;
import com.kalix.training.hotlists.api.dto.HotTeacherDTO;
import com.kalix.training.hotlists.api.dto.HotlistsCourseDTO;
import com.kalix.training.hotlists.api.dto.HotlistsTeacherDTO;
import com.kalix.training.hotlists.api.dto.SelectedCourseDTO;
import com.kalix.training.hotlists.biz.utils.JsonUtil;
import com.kalix.training.hotlists.entities.HotlistsCourseBean;

import java.util.ArrayList;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

public class HotlistsCourseBeanServiceImpl extends ShiroGenericBizServiceImpl<IHotlistsCourseBeanDao, HotlistsCourseBean>
implements IHotlistsCourseBeanService {

    /**
    * 推荐课程（关联表：课程表，用户表，课程类别表）
    * @return jsondata
    */
    @Override
    public JsonData getAllHotlistsCourse() {
        String sql = "select h.id,h.cid,h.identify,c.introduction,c.points,c.name as cname,c.classhour,c.credit,c.photo,u.name as uname,ct.name as gname from mooc_hotlistscourse h,mooc_course c,sys_user u,mooc_coursecategory ct where h.cid = c.id and c.uid = u.id  and c.gid = ct.id ORDER BY h.identify";
        List<HotlistsCourseDTO> list = dao.findByNativeSql(sql, HotlistsCourseDTO.class);
        JsonData jsonData = new JsonData();
        jsonData.setData(list);
        jsonData.setTotalCount((long) list.size());
        return jsonData;
    }

    /**
     * 获取推荐管理中所有数据包括热门教师
     * @return jsondata
     */
    @Override
    public String getHotlistsAllData() {
        List<HotlistsCourseDTO> hclist = dao.findByNativeSql("select h.id,h.cid,h.identify,c.introduction,c.points,c.name as cname,c.classhour,c.credit,c.photo,u.name as uname,ct.name as gname from mooc_hotlistscourse h,mooc_course c,sys_user u,mooc_coursecategory ct where h.cid = c.id and c.uid = u.id  and c.gid = ct.id ORDER BY h.identify", HotlistsCourseDTO.class);
        List<HotlistsTeacherDTO> htlist = dao.findByNativeSql("select t.id,t.identify,t.uid,u.name as uname,u.icon from mooc_hotliststeacher t,sys_user u where u.id = t.uid ORDER BY t.identify", HotlistsTeacherDTO.class);
        List<SelectedCourseDTO> sclist = dao.findByNativeSql("select s.id,s.identify,s.cid,c.name as cname,c.photo,c.introduction,c.classhour,c.credit,c.points,ct.name as gname,d.content as dname,u.name AS uname from mooc_selectedcourse s,mooc_course c,sys_user u,mooc_coursecategory ct,mooc_departments d where s.cid = c.id and c.uid = u.id and c.gid = ct.id and c.did = d.id ORDER BY s.identify", SelectedCourseDTO.class);
        List<HotTeacherDTO> hotTealist = dao.findByNativeSql("SELECT sum(v.hits),u.id as uid,u.name,u.icon FROM mooc_videos v,mooc_course c,sys_user u where v.cid = c.id and c.uid = u.id GROUP BY u.id,u.name,u.icon ORDER BY sum desc LIMIT 10", HotTeacherDTO.class);
        SortedMap<Object, Object> params = new TreeMap<Object, Object>();
        params.put("hcData", hclist);
        params.put("htData", htlist);
        params.put("scData", sclist);
        params.put("hotTeaData", hotTealist);
        String json = "";
        json = JsonUtil.toJson(params);
        return json;
    }
}
